package network.e2e;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import network.link.FrameInputStream;
import network.link.FrameOutputStream;

import org.junit.Assert;
import org.junit.Test;

/**
 * JUnit test for Frame streams Input and Output
 */

public class FrameWriterReaderTest {
  
  @Test
  public void testString() throws IOException {
    String[] messages = {
        "Hi! My name is Irvan Jahja! I'm very handsome you know...",
        "Please tell me why.",
        "You dont know? You're hopeless",
        "",
        "ah.."};
    
    ByteArrayOutputStream byteout = new ByteArrayOutputStream();
    
    FrameOutputStream fos = new FrameOutputStream(byteout);
    
    FrameWriter writer = new FrameWriter(fos);
    
    for (int i = 0; i < messages.length; ++i) {
      writer.writeString(messages[i]);
    }
    
    writer.flush();
    
    ByteArrayInputStream bytein = new ByteArrayInputStream(byteout.toByteArray());
    FrameInputStream fin = new FrameInputStream(bytein);
    FrameReader reader = new FrameReader(fin);
    
    for (int i = 0; i < messages.length; ++i) {
      String msg = reader.readString();
      System.out.println(msg);
      Assert.assertEquals("Reader/Writer fail", messages[i], msg);
    }
    Assert.assertEquals("Reader/Writer fail", null, reader.readString());
  }
  
  @Test
  public void testLong() throws IOException {
    Long[] messages = {
        1L,
        -100L,
        10392L,
        483L,
        0L};
    
    ByteArrayOutputStream byteout = new ByteArrayOutputStream();
    
    FrameOutputStream fos = new FrameOutputStream(byteout);
    
    FrameWriter writer = new FrameWriter(fos);
    
    for (int i = 0; i < messages.length; ++i) {
      writer.writeLong(messages[i]);
    }
    
    writer.flush();
    
    ByteArrayInputStream bytein = new ByteArrayInputStream(byteout.toByteArray());
    FrameInputStream fin = new FrameInputStream(bytein);
    FrameReader reader = new FrameReader(fin);
    
    for (int i = 0; i < messages.length; ++i) {
      Long msg = reader.readLong();
      System.out.println(msg);
      Assert.assertEquals("Reader/Writer fail", messages[i], msg);
    }
    Assert.assertEquals("Reader/Writer fail", null, reader.readString());
  }
  
  @Test
  public void testInt() throws IOException {
    Integer[] messages = {
        1,
        -100,
        10392,
        483,
        0};
    
    ByteArrayOutputStream byteout = new ByteArrayOutputStream();
    
    FrameOutputStream fos = new FrameOutputStream(byteout);
    
    FrameWriter writer = new FrameWriter(fos);
    
    for (int i = 0; i < messages.length; ++i) {
      writer.writeInt(messages[i]);
    }
    
    writer.flush();
    
    ByteArrayInputStream bytein = new ByteArrayInputStream(byteout.toByteArray());
    FrameInputStream fin = new FrameInputStream(bytein);
    FrameReader reader = new FrameReader(fin);
    
    for (int i = 0; i < messages.length; ++i) {
      Integer msg = reader.readInt();
      System.out.println(msg);
      Assert.assertEquals("Reader/Writer fail", messages[i], msg);
    }
    Assert.assertEquals("Reader/Writer fail", null, reader.readString());
  }

}
